﻿using UnityEngine;
using System.Collections;

public class Level : MonoBehaviour {

	public GameObject Hero;  
	public GameObject EntryPoint;
	public GameObject ExitPoint;

	public Component[] objectives;


	private ArrayList objectiveList;

	// Use this for initialization
	void Start () {
	
		objectiveList = new ArrayList();

		foreach (Component t in objectives)
		{
			ILevelObjective theObjective = t as ILevelObjective;
			theObjective.SetLevel(this);
			objectiveList.Add(t as ILevelObjective);
		}

		SpawnPlayer();
	}
	
	// Update is called once per frame
	void Update () 
	{
		foreach (ILevelObjective t in objectiveList)
		{
			ILevelObjective theObjective = t;

			if (theObjective.ObjectiveAchieved())
			{
				//end game
				Debug.Log("LLEGO AL FINAL PAPA!");
				Application.LoadLevel("LevelEndedScene");
				return;
			}
			
		}
	}

	private void SpawnPlayer()
	{
		Instantiate(Hero, EntryPoint.transform.position, EntryPoint.transform.rotation);
	}
}
